<?php

namespace App\Controller\Web;

use App\Base\BaseController4Web;
use App\Entity\Question;
use App\Model\QuestionModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

class QuestionController extends BaseController4Web
{
    /**
     * @route("/questions",name="WebQuestionList")
     */
    public function WebQuestionList(){
        $this->initData();
        return $this->render("Web/file_item.html.twig", $this->data);
    }
    /**
     * @route("/question/info",name="WebQuestionInfo")
     */
    public function WebQuestionInfo(){
        $this->initData();
        if (!$id = $this->getParamRequired("id")) {$id=0;}
        $QuestionModel = new QuestionModel($this->getDoctrine());
        $question = $QuestionModel->getById($id);
        if (!$question) {
            $question = $QuestionModel->getRep()->findOneBy(array());
        }
        $this->data["q"] = $question;

        return $this->render("Web/question_info.html.twig", $this->data);
    }


    /**
     * @route("/question/submit",name="WebQuestionUserSubmit")
     */
    public function WebQuestionUserSubmit(){
        $this->initData();
        if (!$content = $this->getParamRequired("content")) {return $this->json_out(0,"");}
        if (!$name = $this->getParamRequired("name")) {return $this->json_out(0,"");}
        $Question = new Question();
        $Question->setStatus(0)
            ->setQuestion($content)
            ->setCreateName($name)
            ->setCreatetime(date("Y-m-d H:i",time()))
            ->setAnswer("等待回复");
        $this->myEm()->persist($Question);$this->myEm()->flush();
        return $this->json_out(1,"提交成功");

    }

}
